//
//  SwiftEntryKitWrapper.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/13.
//

import Foundation
import SwiftEntryKit
import SwiftUI

//弹窗包装器
struct SwiftEntryKitWrapper {
    //显示底部弹窗
    static func showBottomPopup(contentView: @escaping (@escaping () -> Void) -> some View) {
        
        // 创建关闭闭包
        let dismissAction = { SwiftEntryKit.dismiss() }
        
        // 配置弹窗属性
        var attributes = EKAttributes.bottomToast
        
        attributes.hapticFeedbackType = .success
        attributes.entryInteraction = .absorbTouches
        attributes.screenInteraction = .dismiss
        attributes.positionConstraints.verticalOffset = -(UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0)
        // 修改为 empty 并填充安全区域，避免底部安全区域影响布局
        attributes.positionConstraints.safeArea = .overridden
        attributes.displayDuration = .infinity
        // 添加灰色背景配置// 灰色 + 透明度
        attributes.screenBackground = .color(color: EKColor(
            UIColor(white: 0.3, alpha: 0.7)
        ))
        // 关键配置 1：强制填满屏幕宽度
        attributes.positionConstraints.size = .init(
            width: .fill,       // 水平方向填满屏幕
            height: .intrinsic  // 垂直方向自适应内容高度
        )
        
        let swiftUIView = contentView(dismissAction)
        
        
        // 将 SwiftUI 视图转换为 UIKit 视图
        let hostingController = UIHostingController(rootView: swiftUIView)
        // 关键步骤：禁用安全区域调整
        hostingController.view.backgroundColor = .clear // 避免默认白色背景
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        hostingController.view.insetsLayoutMarginsFromSafeArea = false // 禁用安全区域边距
        
        // 显示弹窗
        SwiftEntryKit.display(entry: hostingController.view, using: attributes)
    }
    
    //显示底部弹窗
    static func showTopPopup(contentView: @escaping (@escaping () -> Void) -> some View) {
        
        // 创建关闭闭包
        let dismissAction = { SwiftEntryKit.dismiss() }
        
        // 配置弹窗属性
        var attributes = EKAttributes.topFloat
        
        attributes.hapticFeedbackType = .success
        attributes.entryInteraction = .absorbTouches
        attributes.screenInteraction = .dismiss
        // 修改为 empty 并填充安全区域，避免底部安全区域影响布局
        attributes.positionConstraints.safeArea = .overridden
        attributes.displayDuration = .infinity
      
        // 关键配置 1：强制填满屏幕宽度
        attributes.positionConstraints.size = .init(
            width: .fill,       // 水平方向填满屏幕
            height: .intrinsic  // 垂直方向自适应内容高度
        )
        
        let swiftUIView = contentView(dismissAction)
        
        
        // 将 SwiftUI 视图转换为 UIKit 视图
        let hostingController = UIHostingController(rootView: swiftUIView)
        // 关键步骤：禁用安全区域调整
        hostingController.view.backgroundColor = .clear // 避免默认白色背景
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        hostingController.view.insetsLayoutMarginsFromSafeArea = false // 禁用安全区域边距
        
        // 显示弹窗
        SwiftEntryKit.display(entry: hostingController.view, using: attributes)
    }
    
    //显示中间弹窗
    static func showContentPopup(contentView: @escaping (@escaping () -> Void) -> some View) {
        
        // 创建关闭闭包
        let dismissAction = { SwiftEntryKit.dismiss() }
        
        // 配置弹窗属性
        var attributes = EKAttributes.centerFloat
        
        attributes.hapticFeedbackType = .success
        attributes.entryInteraction = .absorbTouches
        attributes.screenInteraction = .absorbTouches //禁止外部取消
        attributes.scroll = .disabled  //禁止滚动
        // 修改为 empty 并填充安全区域，避免底部安全区域影响布局
        attributes.positionConstraints.safeArea = .overridden
        attributes.displayDuration = .infinity
        // 设置屏幕背景为半透明黑色
        attributes.screenBackground = .color(color: .init(
            light: UIColor(white: 0.0, alpha: 0.6),  // 浅色模式
            dark: UIColor(white: 0.0, alpha: 0.6)    // 深色模式
        ))
        
        // 透明缩放进入动画
        attributes.entranceAnimation = .init(
            translate: .init(duration: 0),  // 无位置移动
            scale: .init(from: 0.9, to: 1.0, duration: 0.3),  // 从 90% 缩放至 100%
            fade: .init(from: 0.0, to: 1.0, duration: 0.3)  // 从透明到不透明
        )

        // 透明缩放退出动画
        attributes.exitAnimation = .init(
            translate: .init(duration: 0),  // 无位置移动
            scale: .init(from: 1.0, to: 0.9, duration: 0.2),  // 从 100% 缩放至 90%
            fade: .init(from: 1.0, to: 0.0, duration: 0.2)  // 从不透明到透明
        )
      
        // 关键配置 1：强制填满屏幕宽度
        attributes.positionConstraints.size = .init(
            width: .intrinsic,       // 水平方向自适应内容高度
            height: .intrinsic  // 垂直方向自适应内容高度
        )
        
        let swiftUIView = contentView(dismissAction)
        
        
        // 将 SwiftUI 视图转换为 UIKit 视图
        let hostingController = UIHostingController(rootView: swiftUIView)
        // 关键步骤：禁用安全区域调整
        hostingController.view.backgroundColor = .clear // 避免默认白色背景
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        hostingController.view.insetsLayoutMarginsFromSafeArea = false // 禁用安全区域边距
        
        // 显示弹窗
        SwiftEntryKit.display(entry: hostingController.view, using: attributes)
    }
}
